package lms.view;

import lms.entity.Reader;
import lms.entity.ReaderType;
import lms.service.BorrowService;
import lms.service.OperationService;
import lms.service.impl.BorrowServiceImpl;
import lms.service.impl.ReaderServiceImpl;
import lms.utils.VerificationInput;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @description:
 * @author: Grape_Pip
 * @time: 2021/06/21
 */
public class ManageReaderView {
    public Scanner scan = new Scanner(System.in);
    public OperationService operationService = new ReaderServiceImpl();
    public BorrowService borrowService = new BorrowServiceImpl();
    boolean res = false;

    /**
     * @Description //TODO  读者管理操作选择
     **/
    public ManageReaderView() throws ParseException {
        readerOperation();//调用图书管理操作
    }

    /**
     * @return void
     * @Description //TODO  管理读者操作具体实现
     * @Param []
     **/
    public void readerOperation() throws ParseException {
        ReaderManageShow();
        System.out.println("请输入您要选择的操作：");
        String rOption = scan.next();
        switch (rOption) {
            case "1":
//                查询
                List<Reader> readerslist = operationService.selectedAllInfo();
//                循环输出
                System.out.println("| 查询结果为：");
                if (readerslist != null && !readerslist.isEmpty()) {
                    for (Reader readers : readerslist) {
                        System.out.println(readers);
                    }
                } else {
                    System.out.println("图书库空空如也~~~");
                    System.out.println();
                }
                readerOperation();
                break;
            case "2":
//                删除 前需要判断是否有人借阅这本书
                System.out.println("请输入需要删除的读者ID:");
                int rid;
                while (!scan.hasNextInt()) {
                    String stu = scan.next();
                    System.out.println("请输入纯数字(⊙o⊙)！");
                    System.out.println("请重新输入ID吖：");
                }
                rid = scan.nextInt();
                boolean result = borrowService.selectedByRid(rid);
                if (result) {
                    System.out.println("此读者有借阅记录，请核实是否有未归还的书籍！");
                } else {
                    res = operationService.deletedById(rid);
                    if (res) {
                        System.out.println("删除成功！");
                    } else {
                        System.out.println("无此读者信息，删除失败！");
                    }
                }
                readerOperation();
                break;
            case "3":
//                增加
//                输入需要增加的图书信息
                Reader readerInfo = scannerReaderInfo();
                res = operationService.insertedInfo(readerInfo);
                if (res) {
                    System.out.println("插入成功!");
                } else {
                    System.out.println("插入失败!");
                }
                readerOperation();
                break;
            case "4":
//                更新 编号不变
                System.out.println("请输入您需要修改的学生编号：");
                int rId;
                while (!scan.hasNextInt()) {
                    String stu = scan.next();
                    System.out.println("请输入纯数字(⊙o⊙)！");
                    System.out.println("请重新输入吖：");
                }
                rId = scan.nextInt();
                Reader updatedReaderInfo = scannerReaderInfo();
                updatedReaderInfo.setReaderId(rId);

                res = operationService.updatedById(rId, updatedReaderInfo);
                if (res) {
                    System.out.println("更新成功!");
                } else {
                    System.out.println("更新失败!");
                }
                readerOperation();
                break;
            case "0":
                new ManageView().mOperation();
                ;//上一层
            default:
                System.out.println("无此操作哦~");
                readerOperation();
                break;

        }
    }

    /**
     * @return lms.entity.Book
     * @Description //TODO  控制台插入读者信息的reader对象
     * @Param []
     **/
    public Reader scannerReaderInfo() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Reader rd = new Reader();
        Scanner scanner = new Scanner(System.in);
        boolean flag = false;
        System.out.println("》请输入读者姓名：");
        rd.setReaderName(scanner.next());

        do {
            System.out.println("》请输入读者性别：（男/女）");
            String rgender = scan.next();
            if (rgender.equals("男") || rgender.equals("女")) {
                rd.setReaderGender(rgender);
                flag = false;
            } else {
                System.out.println("请按照要求填写性别！");
                flag = true;
            }
        } while (flag);

        do {
            System.out.println("》请输入读者联系电话：");
            String tel = scanner.next();
            if (VerificationInput.isMobileNO(tel)) {
                rd.setReaderTel(tel);
                flag = false;
            } else {
                System.out.println("请按照要求输入联系方式吖~（11位）");
                flag = true;
            }
        } while (flag);

        System.out.println("》请输入读者专业:");
        rd.setReaderMajor(scanner.next());
        //类型
        rd.setReaderType(new ReaderType(rTypeOption(), 0));
        //注册日期
        rd.setReaderRegisDate(new Date());
        return rd;
    }

    /**
     * @return void
     * @Description //TODO  管理读者功能展示
     * @Param []
     **/
    public void ReaderManageShow() {
        System.out.println("^========图书馆管理系统 v2.0========^");
        System.out.println("<========     读者管理     ========>");

        System.out.println("|---------- 1、查询读者 -----------|");
        System.out.println("|---------- 2、删除读者 -----------|");
        System.out.println("|---------- 3、增加读者 -----------|");
        System.out.println("|---------- 4、更新读者 -----------|");
        System.out.println("|---------- 0、返回上一层 -----------|");
        System.out.println();
    }

    /**
     * @return java.lang.String [rType]
     * @Description //TODO  读者选择类型
     * @Param []
     **/
    public String rTypeOption() {
        String rType = null;
        System.out.println("》请输入读者类型:(1、一类  2、二类  3、三类)");
        int rtype;
        while (!scan.hasNextInt()) {
            String stu = scan.next();
            System.out.println("请输入纯数字(⊙o⊙)！");
            System.out.println("请重新输入吖：");
        }
        rtype = scan.nextInt();

        switch (rtype) {
            case 1:
                rType = "一类";
                break;
            case 2:
                rType = "二类";
                break;
            case 3:
                rType = "三类";
                break;
            default:
                System.out.println("没有此选项，请重新输入：");
                rTypeOption();
        }
        return rType;
    }
}
